package com.pounds.service.impl;

import com.pounds.mapper.UserMapper;
import com.pounds.mapper.WorkMapper;
import com.pounds.pojo.Work;
import com.pounds.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkServiceImpl implements WorkService {

    @Autowired
    private WorkMapper workMapper;

    @Autowired
    private UserMapper userMapper;

    enum WorkEnumError {
        Normal,
        DataFormatError,
        EmptyError,
        DataNotFoundError
    }

    @Override
    public List<Work> getWorkList() {
        return workMapper.getWorkList();
    }

    @Override
    public String addWork(int user, String event, String need, String note) {
        if (event == null || user < 0) {
            return WorkEnumError.DataFormatError.toString();
        } else if (event.isEmpty()) {
            return WorkEnumError.EmptyError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return WorkEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return WorkEnumError.DataNotFoundError.toString();
        }
        workMapper.addWork(user, name, event, need, note);
        return WorkEnumError.Normal.toString();
    }

    @Override
    public String deleteWork(int id) {
        List<Work> work = workMapper.selectWork(id);
        if (work == null) {
            return WorkEnumError.DataNotFoundError.toString();
        }
        workMapper.deleteWork(id);
        return WorkEnumError.Normal.toString();
    }

    @Override
    public String updateWork(int id, int user, String event, String need, String note) {
        if (event == null || user < 0) {
            return WorkEnumError.DataFormatError.toString();
        } else if (event.isEmpty()) {
            return WorkEnumError.EmptyError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return WorkEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return WorkEnumError.DataNotFoundError.toString();
        }
        workMapper.updateWork(id, user, name, event, need, note);
        return WorkEnumError.Normal.toString();
    }
}
